Macromedia Flash MX 2004 Timeline Effects
This three-part article covers the basics of Timeline effects, both using them and creating them. We dissect two of the built-in Macromedia Timeline effects (Drop Shadow and Transition) by going over the Macromedia code used to build them. The first section should cover everything you need to get started with Timeline effects. It assumes no prior experience, not even programming experience. For the code dissection, a familiarity with ActionScript or JavaScript is pretty much required because the code itself is pretty advanced. If you are already familiar with scripting in Flash, you might want to skip the basics here and jump directly to the code dissection section.
Please keep in mind that while these effects may or may not apply to problems you solve daily, they are intended as starting points, not destinations. With a little work, or even just a little tweaking, you can end up with a catalog of effects that suits your needs very well.
The very best thing about the latest version of Flash is that you can automate all those things in life that are boring, repetitive, and fraught with error. According to recent congenial science research, people have just two modes of activity: when they behave like people and when they behave like computers. Creating Flash files certainly involves a bit of both. While we love to dwell on things like our creativity and precious flair, this hides the greater truth: We make lousy computers.
Enter the Flash automation tools. Timeline effects, commands, behaviors, custom tools, the History panel, and even custom DLLs. As these automation tools gain traction in the Flash world, you will see more and more of your daily repetitive drudgery go the way of the slide rule. If you know someone whose job involves a lot of, say, nudging symbols frame by frame eight hours a day, then mastering Timeline effects would be great career advice to impart upon them—that, or master those new thermostatic side-feed dishwashers. Whatever your eventual career path, it is always good to have a leg up on the competition.
Timeline effects are only one of many automation options. However, underneath you will find most of this area fairly similar in concept, structure, and code. Helpfully, the script at the heart of Flash automation is JavaScript. As you probably know, this is essentially the same as ActionScript, the scripting language of Flash. If you are new to programming, this is a great way dabble somewhat and get the hang of things. It is set up so that a little bit of programming knowledge can go a long way. If you have been programming Flash for a while, or even scripting HTML pages, this will be pretty familiar territory—which is always good news. As you will see, the transition between using Flash and scripting Flash can be very gradual indeed. Most importantly, each step results in real work being made easier.
Before looking at the internal workings of Timeline effects, you should be sure you are familiar using them. Let's break "using them" into three parts: adding them to your work, editing them, and removing them.
Adding Timeline Effects
As it happens, all of the built-in Timeline effects require an object to be selected on the Stage. Of course you could create your own command or tool that simply operates from scratch but the idea of Timeline effects is to operate on existing objects. For now, you can just create one of those ever-popular red ovals on the Stage and select it. Wait, dare to dream: Let's make a few ovals. The effect you create will be applied to this graphic.

All of the Timeline effect options are stored in two submenus, both called (not surprisingly) Timeline Effects. You can find these under the Insert and Modify menus (or off the context menu that appears when right- or Ctrl-clicking an object). Within the Insert > Timeline Effects menu, the built-in effects are categorized in three submenus: Assistants, Effects, and Transform/Transitions. Try applying a drop shadow (in the Effects submenu). If this menu is grayed out, it is most likely because your graphic wasn't selected:

This brings up an edit screen—the main interface you use to interact with Timeline effects. Here you can adjust the offset, alpha, and color of the shadow. You can also preview how it looks (although you must Update Preview to see the changes). If this interface seems familiar, it should because it is made with Flash using the built-in components. Once you set it just the way you like it, click OK...and voilà!

One of the neat things about Timeline effects is that they are considered a single transaction. This means that they can be undone with a single undo. If you look in the History panel (press Ctrl+F10) and you will see that step listed as "Run Effect." You can see how it's just a single step if you undo and redo it. This may not seem like such a big deal with a drop shadow on an oval but for more complex effects it can be very helpful.

The other neat thing is that when you choose the parameters of an effect (color, offset, and alpha in this case), you have more than one chance to get it right. While the first urge might be to drill into the symbol and adjust the shadow manually, you will find that double-clicking doesn't work and Edit > Symbol (Ctrl+E) throws up the warning: "You are trying to edit a symbol that has an effect applied. If you choose to proceed, you will lose the ability to edit its settings. Do you want to continue?" Now of course you can continue and edit things manually, but you will probably be happier editing it through its interface. To do this, select the symbol that has the Timeline effect applied to it and open the Properties panel (Ctrl+F3). You can see which effect has been applied here. By clicking the Edit button, you can bring up the original dialog box showing the current properties. By adjusting them, you will reapply them.
What Makes Timeline Effects Work
So how does this all happen? With script, of course, but what makes most Flash JavaScript so easy to understand is that it does exactly what you could do yourself in Flash. If you can add a keyframe, it can add a keyframe; if you can add a library item to the Stage, so can it. The first thing to look at is the evidence of what it did:

First look in the library. Here you will find that a new symbol has been created, named something like "Drop Shadow 1." This resulting symbol includes your original graphic with the drop shadow applied to it. Second, there is a folder called Effects Folder, which stores all the builder symbols. You can see that effectSymbol is your original symbol. This has to be constructed because your original selection might have consisted of more than one symbol or, as in your case, just raw graphics. Anyway, it is easy to see that there was some work done in the library, much like there would have been had you made your own drop shadow—except that things are named properly and tucked neatly into folders here.
Next check the innards of the new symbol. If you edit it (drilling through the warning dialog box) you can see that there are, in fact, two symbols in here: one containing the original graphics and one that's a duplicate of it. The duplicate becomes the shadow, offset and tinted/alpha'd per your settings.
Had you done this yourself, it would have gone something like:
- Select things that need the shadow.
- Make a symbol from these.
- Store the symbol in a working folder and call it whatever.
- Duplicate this symbol.
- Offset it.
- Tint it to 100% with your desired shadow color.
- Set the transparency.
- Select the original and the shadow.
- Create a new symbol and call it something like "Drop Shadow X."
As you can see, if you had to do 20 of these in a row, you would need to enter that human comatose state—getting into a little pattern and most likely getting some of the names or steps wrong. Although errors are not catastrophic here, they do trip you up and require you to double-check each mundane step. Sounds like something ripe for automation, and so it is.
Making Your Own Timeline Effect
You can see from just using the Drop Shadow effect that a few things have to be in place before this works. The most obvious is that there has to be code to make this happen. Then you need to make that Edit screen. You also somehow need to tell the Flash menu system to add the Drop Shadow effect to the Timeline Effects > Effects menu. Finally, you need to keep track of all the values—a way of passing the current effect's parameters back and forth to the edit box. We will cover these four things, starting with the easiest, of course. This way, if you get hit by a bus before reading the hard part, you will not have struggled through it for nothing. Always think of the big picture.
Adding a Menu Item
Normally, adding a menu item to a program you didn't write would be a pretty tricky task. Happily, Macromedia foresaw this need and made this step the easiest one. There are special configuration folders that Flash checks every time it opens; if you add information about your menu item there, it appears in the appropriate menu. The hardest part is finding and opening that folder; it is somewhat of a buried treasure. Its location depends on your language and operating system:
Windows 2000 or Windows XP |
Windows 98 |
|
Mac OS X |
As you can see once you arrive at that folder, there are a fair number of things going on. Much of Flash's amazing flexibility starts here—tools, templates, settings, updates ... The folder you are concerned with here is Effects. If you sort by file type you can see that there are three types of files: JSFL, SWF, and XML. They mostly come in sets, one of each type (same name, different extensions). If you sort by name you can see the triplet sets better:
Who can resist checking these files out—especially clicking on those SWFs?! Trying out dropshadow.swf should be enough to convince you that these SWFs are used for the interface. You can ignore the JSFL files for a moment, except to note that JSFL stands for something like "JavaScript Flash"—meaning the code. Lastly comes the XML file. As you probably suspect, this is the glue file that holds everything together—matches things up, sets various parameters, and lets Flash know what to do with it all. This includes telling Flash to add a menu, what to call it, and which subfolder to put it in. If this thought doesn't bring out the hacker in you, try spending less time outdoors.
Opening the XML file (in any text editor) shows that this should be pretty straightforward. At the top you have "group"; the next level is "effect." Both of their name properties correspond to the submenu and menu item of the Drop Shadow effect (Insert > Timeline Effects > Effects > DropShadow). Try one of your own. Add a group name and an effect name, seeing as the rest seems to require what you might call a working Timeline effect. Save dropshadow.xml as test.xml (in the same folder) and delete everything except the <group> and <effect> tags. Change the names to whatever you like, so you end up with something like this:
<group name="Compliment"> <effect name="Oh you beauty!" /> </group>
Save it and restart Flash. Such excitement! Make a new file and then go to Insert > Timeline Effects. There it is: a Compliment menu. Sure enough, Flash has something nice to say inside it. Every step results in real work being made easier indeed; think how hard you usually have to work to get a compliment like that. Of course, clicking it causes an error but don't let that dampen your little celebration here. Much of the Flash interface can be owned by you—easily, in fact. This is a big deal.
With only slightly more effort, you can create your own dialog box. As the dropShadow.xml file suggests, this is done with <dialog> tags, so add a line like so:
<group name="Compliment"> <effect name="Oh you beauty!" /> <dialog id="xx" title="Message from a fan" buttons="accept,cancel"> </dialog> </group>

You
may need to restart Flash before this takes effect. Available effects are
loaded when Flash starts up so it requires a restart if the structure changes.
Otherwise you can make a command that says fl.reloadEffects(); and
run that. In either case, you should now see a dialog box that works perfectly,
as long as you click Cancel. The error you get by clicking OK occurs because
there is no script to run. Scanning dropShadow.xml
again, you can see that adding a <source> tag is probably the ticket:
<group name="Compliment"> <effect name="Oh you beauty!" /> <source file="test.jsfl" /> <dialog id="xx" title="Message from a fan" buttons="accept,cancel"> </dialog> </group>
Restart Flash. Much better now: Clicking OK results in the error that test.jsfl cannot be found. This is not surprising because you haven't made it yet. Other than the parts not working, things are working perfectly. Take some time to admire that Cancel button.
Adding the Code
Before diving into the code, take a look at dropshadow.jsfl. Normally JSFL files are like other JavaScript files—just plain text. Most JSFL you see or create is also plain text, which works fine. The Timeline Effects that ship with Flash however, have been obfuscated—encrypted so they are no longer human-readable. Not to worry: You can just write yours in plain JSFL and it will work normally. The takeaway here is that your code will go in this folder and it will have the .jsfl extension.
The first thing you must understand when starting any program is "where does it start?". In the old days this was pretty simple: Programs had line numbers and they started at line 1 (or was it line 0?). In a Flash file this is the same in general: Things begin on frame 1, line 1. Thanks to the many great advancements in computer science, this is now a bit more complicated: Things generally begin by calling some type of predefined function. This is how Flash automation works as well. In fact, there are two points of entry you will deal with, both defined as functions. These functions must be named executeEffect and removeEffect. Obviously, executeEffect makes things happen and removeEffect puts things back the way they were. This would be harder to get right if things always started at line 1. Ah, progress.
The function that makes things happen looks like this:
function executeEffect() { }
Note for nonprogrammers:
If you are brand new to programming, just know that when someone says
executeEffect, the code that is trapped between the curly braces
will run. Don't bust a shear pin trying to understand every detail;
just do as everyone else does and poke around a bit and see what happens.
Totally immature things like putting swear words in title bars is
a solid first step on a path to greatness.
Note for non–JSFL programmers too:
Here is a great way to learn how JSFL works. Do a few things in Flash—create
some of those famous red ovals, move them, skew them, add a frame,
etc. Now select those steps in the History panel (Shift-click) and "copy
steps" by
clicking the little copy icon in the bottom-right corner. Paste this
into a text editor and voilà! After you put this code into the
executeEffect function, that is what will happen when you run your
Timeline effect. It can be that simple. Of course, it could also be
much better but this is a still great way to create quick sample/base
code.
Put just one line of the old JavaScript standby in there—that foxy Alert box. So your JSFL code now looks like this:
function executeEffect() { alert("How beautiful art thou, let me count the ways!"); }
That's it. Save this file in the same Effects Folder and call it test.jsfl; that is what you said it would be called in the <source> tag earlier. Run your effect now (you might not need to restart here) and ... look at that: A fully working Timeline function. It doesn't alter your Timeline but it may raise your self-esteem. It need not end here—you could get a full mood dial in there, or sneak helpful suggestions into the interface of your coworkers. The possibilities are endless:
Just to make this complete, you should actually alter the Timeline. While you clearly do not have to, a Timeline effect that doesn't alter the Timeline should really be a command instead. So let's add some frames. A quick trip to the History panel tells you how this is done:
fl.getDocumentDOM().getTimeline().insertFrames();
Assuming that insertFrames allows a number (it does), your code now looks like this:
function executeEffect() { fl.getDocumentDOM().getTimeline().insertFrames(20); alert("How beautiful art thou, let me count the ways!"); }
Running this, you can see indeed that 20 frames have been inserted. One thing you might have noticed already is that these Timeline effects are always wrapped in a symbol automatically and that symbol is added automatically to the library. This is a good thing because it helps contain these effects, allows you to tween multiple selections, and so on. Like all good things, however, they work best when you know about them in advance, so keep this in mind.
You may have noticed that the Edit button (on the Properties toolbar) is functional whenever your effect is selected. It brings up your mighty dialog box, with its two mighty buttons. Clearly this area needs some work. Maybe later you can change this to illustrate the phase shifts of radioactive deuterium or maybe just allow you to specify the number of frames to insert. First the basics, though; you can jump off that bridge when you get to it.
Adding the Editor
While the interfaces used to create and edit the Timeline effects are all SWFs, that isn't the whole truth. In fact, all of these types of user interfaces are actually made with a markup language based on XUL. First thing to know about XUL is that it is just a straightforward way of describing how simple windows or dialog boxes should look. Unfortunately, this technology was created in the 1998–2002 period when XML was at its peak of fashion and everything needed an X in the name. It really should have been called something like UIML or, even better, XMLUI (for XML User Interfaces). Enter Macromedia, which calls it XMLUI ... thank you. This isn't full-blown XUL, however. There are a few specific tags added; hence the different name.
This is a pretty cool and simple way to make an interface: You just add tags for the elements you want, insert some layout hints if you like, and things just work. The version in Flash is much simpler than the original so it is very easy (and fun) to use. The XMLUI stuff you have in there so far is in the XML file:
<dialog id="test_dialog" title="Message from a fan" buttons="accept,cancel">
</dialog>
This code creates a dialog box with attributes allowing for a title and default buttons. All XMLUI interfaces must happen inside <dialog> tags. So what else can you put in there? Well, most of the common things like buttons, check boxes, drop-down lists, text boxes, sliders ... and a few unusual ones, like a color picker and Flash movies. Whoa, Flash movies? That is the secret to a SWF-based UI: Use a Flash movie inside your <dialog> tags. For your first try, though, just use plain XMLUI markup because it's more "pure." The concepts are exactly the same for SWF interfaces; only the communication is different.
Let's get fancy and use a slider here. There may be better ways of doing this, but not simpler ones! Start with this code:
<dialog id="test_dialog" title="Message from a fan" buttons="accept,cancel">
<popupslider id="frameCount" minvalue="1" maxvalue="100" />
</dialog>

You inserted a slider named frameCount, which holds a value between 1 and 100. You could run it and take a look, just to make sure it does indeed make a slider. (You aren't doing anything with the slider value yet but it's still pretty nifty.) What you need is a way to get that value into your script. For this to happen, you first have to make the frameCount value visible to the outside world. Do this by making it a property. So now your full test.xml file looks like this (note the new <properties> tag):
<group name="Compliment"> <effect name="You Beauty" /> <source file = "test.JSFL" /> <properties> <property name="Frames to Insert" variable="frameCount" min="1" defaultValue="100" type="Number" /> </properties> <dialog id="test_dialog" title="Message from a fan" buttons="accept, cancel" > <popupslider id="frameCount" minvalue="1" maxvalue="100" /> </dialog> </group>
To find that value in your code, you need to find your effect. You can assume this is the current effect because otherwise it would not be running! Looking this up in the docs, you can see that it is fl.activeEffect so if you add the property name to this, you should be good to go. (Altogether it looks like fl.activeEffect.frameCount.) If you spruce it up a little, your script now looks like this:
function executeEffect() { var fc = fl.activeEffect.frameCount; // false means current layer only, 1 means start on frame 1 fl.getDocumentDOM().getTimeline().insertFrames(fc, false, 1); alert( "How beautiful art thou, let me count the ways!\n" + "Let's see, that would be a total of " + fc + " ways."); }
It counts the ways it loves you and then inserts that number of frames. (Amazingly enough, we could've used this exact tool yesterday just before lunch and here it is sitting in this tutorial.)
There is one tiny problem left. You might have noticed that if you go back into the editor (by clicking the Edit button in the Properties panel when it is selected) and change the value, it doesn't change the number of frames—in fact, it adds the new number of frames! Clearly it's not undoing your original number of frame insertions before adding the new ones. Perhaps it just loves you too much—or perhaps you should look into a way to unwind things. If you remember way back, we mentioned that there are two predefined functions that Flash knows to call: executeEffect and removeEffect. Now it seems that removeEffect might be what you are looking for because you need to remove it before reapplying it. Stick that function in your code to see what happens when it runs:
function removeEffect() { alert("remove"); }
Notice the steps. You create the effect normally. Click Edit and you can adjust things as before. When you click OK, however, it first runs removeEffect and then it runs executeEffect. You should be able to unwind whatever damage you did using the removeEffect function and then inflict it again with new settings in the same executeEffect function.
If you were to do this by hand, it would be fairly easy: Just delete all frames but the first one. If you were polite you would clean up the library, too, but fortunately you aren't afflicted with such a disposition, time-saver god that you are.
function removeEffect() { // it takes typing to save typing var tl = fl.getDocumentDOM().getTimeline(); var curLayer = tl.layers[tl.currentLayer]; // remove all frames but the first tl.removeFrames(1, curLayer.frameCount - 1); // recant our feelings alert("I take that back, you're a bit plain."); }
Now run this. Hooray, it works more or less. It isn't 100% robust; it leaves junk in the library and it encounters a tiny problem if you start in the middle of a Timeline. Of course, the nice thing about creating a custom tool is that it's just for yourself, not for clients or the world at large. You can be as much of a hacker as you want. Besides, you are beautiful so your tools don't have to be.
We look at a couple of fully robust tools from Macromedia in the next section. These were designed with the big picture in mind, so they were carefully spec'd to take into account all possible issues and then meticulously coded. That's why they get the big bucks. However, because they are only slightly more complex than the effect you have just made, the next section will be considerably more difficult if you are a beginner. You may be happier trying a few of your own first or finding a few simple examples on the web. It won't hurt to have a look, though; just keep in mind that this is close to "as hard as it gets"—useful in-house effects do not have to be nearly so robust.
Next: The Drop Shadow Effect
Documentation
- Live Docs
- JSAPI Docs
- Flash JavaScript Dictionary (PDF)
- JSAPI Update (flash_jsapi_help.mxp)
- JavaScript Developer Central (Netscape)
Mailing Lists
- ExtendFlash mailing list (FlashGuru)
External Links
- Introduction to the JSAPI (Guy Watson)
- Extending Flash MX 2004 (Keith Peters)
- XML to UI version 2.2 (Peter Brouwers)